﻿using GalaSoft.MvvmLight;

namespace AzureDiagMon.Common.Mvvm
{
    /// <summary>
    /// Generic view model for entities
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public class EntityViewModel<TEntity> : ViewModelBase where TEntity : class, new()
    {
        private readonly TEntity entity;

        public TEntity Entity
        {
            get { return entity; }
        }

        private bool isDirty;
        public bool IsDirty
        {
            get { return isDirty; }
            set
            {
                isDirty = value;
                RaisePropertyChanged("IsDirty");
            }
        }

        public bool IsNew { get; private set; }

        public EntityViewModel(TEntity entity)
        {
            if (entity == null)
            {
                entity = new TEntity();
                IsNew = true;
            }
            else
            {
                this.entity = entity;
            }
        }

        protected void EntityFieldUpdated(string fieldName)
        {
            IsDirty = true;
            RaisePropertyChanged(fieldName);
        }

    }
}
